package com.kmxd.ams.app.use;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.use.IUseLogSvc;
import com.kmxd.ams.client.use.dto.UseLogAddDTO;
import com.kmxd.ams.client.use.dto.UseLogEditDTO;
import com.kmxd.ams.client.use.dto.UseLogQueryDTO;
import com.kmxd.ams.client.use.dto.UseOrderManageDTO;
import com.kmxd.ams.client.use.vo.UseLogListVO;
import com.kmxd.ams.client.use.vo.UseLogVO;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.infra.use.entity.UseLog;
import com.kmxd.ams.infra.use.mapper.UseLogMapper;
import java.util.List;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 利用操作日志 服务实现类
 *
 * @author nickbi
 * @since 2022-09-08
 */
@Service
@AllArgsConstructor
public class UseLogSvcImpl extends ServiceImpl<UseLogMapper, UseLog> implements IUseLogSvc {

  private UseLogMapper useLogMapper;

  @Override
  public Page<UseLogListVO> selectPage(Page reqPage, UseLogQueryDTO req) {
    LambdaQueryWrapper<UseLog> queryWrapper =
        QueryGen.init(new QueryWrapper<UseLog>(), req).lambda();
    Page<UseLog> page = useLogMapper.selectPage(reqPage, queryWrapper);
    Page<UseLogListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), UseLogListVO.class));
    return voPage;
  }

  @Override
  public List<UseLogListVO> selectList(UseLogQueryDTO req) {
    LambdaQueryWrapper<UseLog> queryWrapper =
        QueryGen.init(new QueryWrapper<UseLog>(), req).lambda();
    List<UseLog> list = useLogMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, UseLogListVO.class);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(UseLogAddDTO req) {
    UseLog entity = BeanUtil.copyProperties(req, UseLog.class);
    useLogMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(UseLogEditDTO req) {
    UseLog entity = BeanUtil.copyProperties(req, UseLog.class);
    useLogMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    this.baseMapper.deleteBatchIds(idList);
  }

  @Override
  public UseLogVO view(Long id) {
    UseLog entity = useLogMapper.selectById(id);
    return BeanUtil.copyProperties(entity, UseLogVO.class);
  }

  @Override
  public void asyncSaveLog(UseOrderManageDTO req) {
    if (ObjectUtil.isEmpty(req.getUseArchIds())) {
      return;
    }
    UseLog log = new UseLog();
    log.setOrderId(req.getId());
    log.setType(req.getType().getType());
    log.setTypeName(req.getType().getDesc());
    String reason = "";
    if (CharSequenceUtil.isNotBlank(req.getReason())) {
      reason = req.getReason();
    }
    reason += req.getRemarks();
    log.setRemarks(reason);
    log.setContent(req.getArchContent());
    log.setArchDataIds(req.getUseArchIds());
    log.setUserName(req.getUserName());
    log.setOpTime(req.getUseDate());
    log.setCreateUserName(req.getCreateUserName());
    log.setCreateBy(req.getCreateBy());
    save(log);
  }
}
